package com.lgj.mall.product.service.impl;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lgj.mall.common.utils.PageUtils;
import com.lgj.mall.common.utils.Query;

import com.lgj.mall.product.dao.CategoryDao;
import com.lgj.mall.product.entity.CategoryEntity;
import com.lgj.mall.product.service.CategoryService;



@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWithTree(Map<String, Object> params) {
        //1.查找所有分类
        List<CategoryEntity> categoryEntities = baseMapper.selectList(null);
        //2.组装成父子结构
        List<CategoryEntity> collect = categoryEntities.stream().filter(
            //过滤一级分类
            categoryEntity -> categoryEntity.getParentCid() == 0
        ).map(cat -> {
            //转化:递归获取子孩子
            cat.setChildrens(getChildrens(cat, categoryEntities));
            return cat;
        }) //排序
        .sorted((cat1, cat2) -> {
            return (cat1.getSort()==null?0:cat1.getSort()) - (cat2.getSort()==null?0:cat2.getSort());
        })//收集
        .collect(Collectors.toList());

        return collect;
    }

    private List<CategoryEntity> getChildrens(CategoryEntity categoryEntity, List<CategoryEntity> categoryEntities) {
       return categoryEntities.stream().filter(
                        cat -> cat.getParentCid().equals(categoryEntity.getCatId())            //cat.getParentCid() == categoryEntity.getCatId()
                ).map(cat -> {
                    cat.setChildrens(getChildrens(cat, categoryEntities));
                    return cat;
                })
                .sorted((cat1, cat2) -> {
                    return (cat1.getSort()==null?0:cat1.getSort()) - (cat2.getSort()==null?0:cat2.getSort());
                })
                .collect(Collectors.toList());
    }

    @Override
    public void removeMenuByIds(List<Long> list) {
        //TODO 1.检查当前删除的菜单是否被引用
//        baseMapper.selectList(new QueryWrapper<CategoryEntity>().in("parent_cid", list)).forEach(
//            cat -> {
//                throw new IllegalArgumentException(cat.getCatId() + "被引用");
//            }
//        );
        //2.删除
        baseMapper.deleteBatchIds(list);
    }

    /**
     * 递归查找分类路径
     * 思路:倒过来找
     */
    @Override
    public Long[] findCatelogPath(Long catelogId) {
        //获取递归父节点(以及父的父...)
        List<Long> paths=new ArrayList<>();
        List<Long> parentPath=findParentPath(catelogId,paths);
        //如果是2 22 225,获得225 22 2  所以需要反转
        Collections.reverse(parentPath);
        //集合转数组
        return parentPath.toArray(new Long[parentPath.size()]);
    }

    private List<Long> findParentPath(Long catelogId,List<Long>  paths) {
        paths.add(catelogId);
        //查找该分类实体
        CategoryEntity entity = getById(catelogId);
        //如果不是一级分类(父节点id是0)
        Long parentCid = entity.getParentCid();
        if (parentCid != 0) {
            findParentPath(parentCid, paths);
        }
        return paths;
    }
}